home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Utilities / PPC / PPCGNUtar / source / sas_stat / stat.c next >
Encoding:
C/C++ Source or Header  |  1997-09-11  |  4.8 KB  |  166 lines

  1. /*
  2. ** mkisofs for the Amiga (SAS/C compiler)
  3. ** by Flavio Stanchina <flavio@ies.it> 3/97
  4. **
  5. ** changed global <stat.h> to local "sas_stat/stat.h"
  6. ** and set as first #include (ARK, 9/97)
  7. **
  8. ** renamed functions (prefix sas_) to get the linker
  9. ** using these instead (also see REDEFINES.WTH) (ARK, 11 Sep 97)
  10. */
  11.  
  12. #include "sas_stat/stat.h"
  13.  
  14. #include <dos/dos.h>
  15. #include <string.h>
  16.  
  17. #include <proto/dos.h>
  18. #include <proto/exec.h>
  19.  
  20. static time_t DateStamp2time(const struct DateStamp *ds)
  21. {
  22.         return (ds->ds_Days + 365*8 + 2) * (24*60*60)
  23.          + ds->ds_Minute * 60
  24.          + ds->ds_Tick / TICKS_PER_SECOND;
  25. }
  26.  
  27. /****** stat(), lstat() *****
  28. *
  29. *   DESCRIPTION
  30. *       stat() obtains information about the file pointed to by path. Read,
  31. *       write or execute permission of the named file is not required.
  32. *
  33. *       lstat() is like stat() except in the case where the named file is a
  34. *       symbolic link, in which case lstat() returns information about the
  35. *       link, while stat() returns information about the file the link
  36. *       references.
  37. *
  38. *   RESULT
  39. *       Upon successful completion a value of 0 is returned. Otherwise, a
  40. *       value of -1 is returned and errno is set to indicate the error.
  41. *
  42. ******/
  43.  
  44. static void fillstat(struct FileInfoBlock *fib, BPTR lock, struct stat *buf)
  45. {
  46.         struct FileLock   *fl = BADDR(lock);
  47.         struct DeviceList *dl = BADDR(fl->fl_Volume);
  48.  
  49.         /* build mode flags */
  50.         mode_t mode = (fib->fib_DirEntryType >= 0) ? S_IFDIR : S_IFREG;
  51.         if (fib->fib_DirEntryType == ST_SOFTLINK) mode = S_IFLNK;
  52.  
  53.         if (fib->fib_Protection & FIBF_OTR_READ)    mode |= S_IROTH;
  54.         if (fib->fib_Protection & FIBF_OTR_WRITE)   mode |= S_IWOTH;
  55.         if (fib->fib_Protection & FIBF_OTR_EXECUTE) mode |= S_IXOTH;
  56.  
  57.         if (fib->fib_Protection & FIBF_GRP_READ)    mode |= S_IRGRP;
  58.         if (fib->fib_Protection & FIBF_GRP_WRITE)   mode |= S_IWGRP;
  59.         if (fib->fib_Protection & FIBF_GRP_EXECUTE) mode |= S_IXGRP;
  60.  
  61.         // NOTE: plain rwed bits are "active low"
  62.         if (!(fib->fib_Protection & FIBF_READ))     mode |= S_IRUSR;
  63.         if (!(fib->fib_Protection & FIBF_WRITE))    mode |= S_IWUSR;
  64.         if (!(fib->fib_Protection & FIBF_EXECUTE))  mode |= S_IXUSR;
  65.  
  66.         /* fill in stat struct */
  67.         buf->st_dev   = fl->fl_Volume;
  68.         buf->st_ino   = fib->fib_DiskKey;
  69.         buf->st_mode  = mode;
  70.         buf->st_nlink = 1;
  71.         buf->st_uid   = fib->fib_OwnerUID;
  72.         buf->st_gid   = fib->fib_OwnerGID;
  73.         buf->st_rdev  = dl->dl_DiskType;
  74.         buf->st_size  = fib->fib_Size;
  75.  
  76.         buf->st_atime =
  77.         buf->st_mtime =
  78.         buf->st_ctime = DateStamp2time(&fib->fib_Date);
  79.  
  80.         buf->st_blocks  = fib->fib_NumBlocks;
  81.         buf->st_comment = fib->fib_Comment;
  82. }
  83.  
  84. int sas_stat(const char *path, struct stat *buf)
  85. {
  86.         static struct FileInfoBlock fib;
  87.         BPTR lock;
  88.         int result = -1;
  89.  
  90.         if ((lock = Lock((STRPTR)path, ACCESS_READ)) != 0)
  91.         {
  92.                 if (Examine(lock, &fib))
  93.                 {
  94.                         fillstat(&fib, lock, buf);
  95.                         result = 0;
  96.                 }
  97.  
  98.                 UnLock(lock);
  99.         }
  100.  
  101.         return result;
  102. }
  103.  
  104. int sas_lstat(const char *path, struct stat *buf)
  105. {
  106.         static struct FileInfoBlock fib;
  107.         BPTR lock;
  108.         int result = -1;
  109.         char tmp[256], *f = FilePart((STRPTR)path);
  110.  
  111.         strcpy(tmp, path);
  112.         *PathPart(tmp) = 0;
  113.  
  114.         if ((lock = Lock(tmp, ACCESS_READ)) != 0)
  115.         {
  116.                 if (Examine(lock, &fib))
  117.                 {
  118.                         while (ExNext(lock, &fib))
  119.                         {
  120.                                 if (stricmp(fib.fib_FileName, f) == 0)
  121.                                 {
  122.                                         fillstat(&fib, lock, buf);
  123.                                         result = 0;
  124.                                         break;
  125.                                 }
  126.                         }
  127.                 }
  128.  
  129.                 UnLock(lock);
  130.         }
  131.  
  132.         return result;
  133. }
  134.  
  135. /****** readlink() ******
  136. *
  137. *   DESCRIPTION
  138. *       readlink() places the contents of the symbolic link path in the
  139. *       buffer buf, which has size bufsiz. readlink() does not append a NUL
  140. *       character to buf.
  141. *
  142. *   RESULT
  143. *       The call returns the count of characters placed in the buffer if it
  144. *       succeeds, or a -1 if an error occurs, placing the error code in the
  145. *       global variable errno.
  146. *
  147. ******/
  148.  
  149. int sas_readlink(const char *path, char *buf, size_t bufsize)
  150. {
  151.         struct DevProc *dvp;
  152.         int result = -1;
  153.  
  154.         if ((dvp = GetDeviceProc((STRPTR)path, NULL)) != NULL)
  155.         {
  156.                 if (ReadLink(dvp->dvp_Port, dvp->dvp_Lock, (STRPTR)path, buf, bufsize))
  157.                         result = strlen(buf);
  158.  
  159.                 FreeDeviceProc(dvp);
  160.         }
  161.  
  162.         return result;
  163. }
  164.  
  165. /*** EOF ***/
  166.